package code.rocky.threadlocal;

import java.lang.ref.WeakReference;
import java.util.concurrent.TimeUnit;

public class ThreadLocalDemo02 {


    public static void main(String[] args) {
        Student student = new Student(new Person("Rocky"));

        Person person = new Person("Rocky-BCRJ"); // 强引用
        Student s1 = new Student(person);
        System.gc();

        try {
            TimeUnit.SECONDS.sleep(1);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println("gc之后: " + student.get());
        System.out.println("gc之后是否为null: " + (student.get() == null));
        System.out.println("gc之后: " + student.get());
        System.out.println("===============测试强引用是否被gc========================");
        System.out.println("s1 gc之后: " + s1.get());
        System.out.println("s1 gc之后是否为null: " + (s1.get() == null));
        System.out.println("s1 gc之后: " + s1.get());
    }

}

class Person  {
    String name;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Person(String name) {
        this.name = name;
    }

    @Override
    protected void finalize() throws Throwable {
        System.out.println("finalize: " + this);
    }

    @Override
    public String toString() {
        return "Person{" +
                "name='" + name + '\'' +
                '}';
    }
}

class Student  extends WeakReference<Person> {

    public Student(Person referent) {
        super(referent);
    }

}
